<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bluetooth MTU Example</title>
</head>
<body>
    <button id="connectButton">Connect to Bluetooth Device</button>
    <button id="setMTUButton" disabled>Set MTU</button>
    <script>
        let bluetoothDevice;
        let server;

        document.getElementById('connectButton').addEventListener('click', async () => {
            try {
                if (!navigator.bluetooth) {
                    console.log('Web Bluetooth is not supported in this browser.');
                    return;
                }

                bluetoothDevice = await navigator.bluetooth.requestDevice({
                    filters: [{ services: ['6e400001-b5a3-f393-e0a9-e50e24dcca9e'] }] // 替换为你需要的服务 UUID
                });

                console.log('Device selected:', bluetoothDevice.name);

                server = await bluetoothDevice.gatt.connect();
                console.log('Connected to GATT server:', server);

                document.getElementById('setMTUButton').disabled = false;
            } catch (error) {
                console.log('Connection failed:', error);
            }
        });

        document.getElementById('setMTUButton').addEventListener('click', async () => {
            if (!server) {
                console.log('Not connected to a Bluetooth device.');
                return;
            }

            try {
                const newMTU = 512; // 你可以设置一个你期望的 MTU 大小
                await server.requestMTU(newMTU);
                console.log('MTU has been updated to:', newMTU);
            } catch (error) {
                console.log('Failed to update MTU:', error);
            }
        });
    </script>
</body>
</html>